﻿using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;

namespace NCD.Cache
{
    /// <summary>
    /// 注入服务扩展
    /// </summary>
    public class ServiceRegister
    {
        /// <summary>
        /// 
        /// </summary>
        public static IServiceProvider Services { get; private set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="services"></param>
        public static void SetServices(IServiceProvider services)
        {
            Services = services;
        }

        /// <summary>
        /// 手动获取注入的对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T GetService<T>() where T : class
        {
            return Services?.GetService<IHttpContextAccessor>()?.HttpContext.RequestServices.GetService<T>();
        }
    }
}
